package adapter;

import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import bean.QuestionTypeBean;
import student.andlei.com.student.R;
import utils.StringUtils;

public class QuestionTypeNumAdapter extends BaseQuickAdapter<QuestionTypeBean,BaseViewHolder>{
    public QuestionTypeNumAdapter(int layoutResId, @Nullable List<QuestionTypeBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final QuestionTypeBean item) {
        helper.setText(R.id.tv_type,item.type_name+":");
        EditText editText = helper.getView(R.id.ed_xuanze);
        if (editText.getTag() != null && editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }
        if(item.inputNum <= 0){
            editText.setText("");
        }else {
            editText.setText(item.inputNum+"");
        }
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.inputNum = StringUtils.str2Int(s.toString());
            }
        };
        editText.addTextChangedListener(textWatcher);
        editText.setTag(textWatcher);
    }
}
